/**
 * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.afterturn.easypoi.excel;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.export.ExcelBatchExportService;
import cn.afterturn.easypoi.excel.export.ExcelExportService;
import cn.afterturn.easypoi.excel.export.template.ExcelExportOfTemplateUtil;

/**
 * excel 导出工具类
 * 
 * @author JueYue
 * @version 1.0 2013-10-17
 */
public class ExcelExportUtil {

	private ExcelExportUtil() {
	}

	/**
	 * @param entity
	 *            定义表格的各种属性
	 * @param pojoClass
	 *            bean
	 * @param dataSet
	 *            数据List
	 */
	public static Workbook exportBigExcel07(ExportParams entity, Class<?> pojoClass, Collection<?> dataSet) {
		ExcelBatchExportService batchService = ExcelBatchExportService.getExcelBatchExport07Service(entity, pojoClass);
		return batchService.appendData(dataSet);
	}

	/**
	 * @param entity
	 *            定义表格的各种属性
	 * @param pojoClass
	 *            bean
	 * @param dataSet
	 *            数据List
	 */
	public static Workbook exportBigExcel03(ExportParams entity, Class<?> pojoClass, Collection<?> dataSet) {
		ExcelBatchExportService batchService = ExcelBatchExportService.getExcelBatchExport03Service(entity, pojoClass);
		return batchService.appendData(dataSet);
	}

	/**
	 * 关闭(表头处理、cell合并、线程变量的摧毁)
	 */
	public static void closeExportBigExcel07() {
		ExcelBatchExportService batchService = ExcelBatchExportService.getExcelBatchExport07Service(null, null);
		batchService.closeExportBigExcel();
	}

	/**
	 * 关闭(表头处理、cell合并、线程变量的摧毁)
	 */
	public static void closeExportBigExcel03() {
		ExcelBatchExportService batchService = ExcelBatchExportService.getExcelBatchExport03Service(null, null);
		batchService.closeExportBigExcel();
	}

	/**
	 * 单sheet单注解实体导出
	 * 
	 * @param entity
	 *            表格属性配置
	 * @param pojoClass
	 *            对应bean
	 * @param dataSet
	 *            数据List(bean数据对象)
	 */
	public static Workbook exportExcel(ExportParams entity, Class<?> pojoClass, Collection<?> dataSet) {
		Workbook workbook = getWorkbook(entity.getType(), dataSet.size());
		new ExcelExportService().createSheet(workbook, entity, pojoClass, dataSet);
		return workbook;
	}

	/**
	 * 得到workbook对象
	 * 
	 * @param type
	 *            类型
	 * @param size
	 *            大小
	 * @return Workbook
	 */
	private static Workbook getWorkbook(ExcelType type, int size) {
		if (ExcelType.HSSF.equals(type)) {
			return new HSSFWorkbook();
		} else if (size < 65536) {
			return new XSSFWorkbook();
		} else {
			return new SXSSFWorkbook();// 防止内存溢出
		}
	}

	/**
	 * 根据Map创建对应的Excel
	 * 
	 * @param entity
	 *            表格标题属性
	 * @param entityList
	 *            Map对象列表
	 * @param dataSet
	 *            Excel对象数据List
	 */
	public static Workbook exportExcel(ExportParams entity, List<ExcelExportEntity> entityList, Collection<?> dataSet) {
		Workbook workbook = getWorkbook(entity.getType(), dataSet.size());
		new ExcelExportService().createSheetForMap(workbook, entity, entityList, dataSet);
		return workbook;
	}

	/**
	 * 一个excel 创建多个sheet
	 * 
	 * @param list
	 *            多个Map key title 对应表格Title key entity 对应表格对应实体 key data
	 *            Collection 数据
	 * @return
	 */
	public static Workbook exportExcel(List<Map<String, Object>> list, ExcelType type) {
		Workbook workbook = getWorkbook(type, 0);
		for (Map<String, Object> map : list) {
			ExcelExportService service = new ExcelExportService();
			service.createSheet(workbook, (ExportParams) map.get("title"), (Class<?>) map.get("entity"), (Collection<?>) map.get("data"));
		}
		return workbook;
	}

	/**
	 * 导出文件通过模板解析,不推荐这个了,推荐全部通过模板来执行处理
	 * 
	 * @param params
	 *            导出参数类
	 * @param pojoClass
	 *            对应实体
	 * @param dataSet
	 *            实体集合
	 * @param map
	 *            模板集合
	 * @return
	 */
	@Deprecated
	public static Workbook exportExcel(TemplateExportParams params, Class<?> pojoClass, Collection<?> dataSet, Map<String, Object> map) {
		return new ExcelExportOfTemplateUtil().createExcleByTemplate(params, pojoClass, dataSet, map);
	}

	/**
	 * 导出文件通过模板解析只有模板,没有集合
	 * 
	 * @param params
	 *            导出参数类
	 * @param map
	 *            模板集合Map<String, Object>
	 * @return
	 */
	public static Workbook exportExcel(TemplateExportParams params, Map<String, Object> map) {
		return new ExcelExportOfTemplateUtil().createExcleByTemplate(params, null, null, map);
	}

	/**
	 * 每个sheet对应一个map,导出到处,key是sheet的NUM 导出文件通过模板解析只有模板,没有集合
	 * 
	 * @param params
	 *            导出参数类
	 * @param map
	 *            模板集合Map<Integer, Map<String, Object>> Integer对应sheet的num
	 * @return
	 */
	public static Workbook exportExcel(Map<Integer, Map<String, Object>> map, TemplateExportParams params) {
		return new ExcelExportOfTemplateUtil().createExcleByTemplate(params, map);
	}

}
